package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

<<<<<<< HEAD
import java.util.List;

=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

<<<<<<< HEAD
    @RequestMapping("listAll")
    @ResponseBody
    public List<Customer> listAll() {

        List<Customer> list = customerService.list();
        return list;
    }

=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
    @RequestMapping("")
    @RequiresPermissions("customer:index")
    @PermissionName("历史客户列表")
    public String index() {
        return "customer";
    }

    @RequestMapping("potential")
    @RequiresPermissions("customer:potential")
    @PermissionName("潜在客户列表")
    public String potential() {
        return "potential_customer";
    }

    @RequestMapping("vip")
    @RequiresPermissions("customer:vip")
    @PermissionName("正式客户列表")
    public String vip() {
        return "vip_customer";
    }

    @RequestMapping("pool")
    @RequiresPermissions("customer:pool")
    @PermissionName("资源池列表")
    public String pool() {
        return "pool_customer";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(CustomerQueryObject qo) {

        PageResult result = customerService.query(qo);

        return result;
    }

    @RequestMapping("save")
    @RequiresPermissions("customer:save")
    @PermissionName("客户保存")
    @ResponseBody
    public JsonResult save(Customer customer) {
        JsonResult result = new JsonResult();
<<<<<<< HEAD
        try {
            customerService.insert(customer);
            result.setMessage("信息保存成功");
        } catch (Exception e) {
=======
        try{
            customerService.insert(customer);
            result.setMessage("信息保存成功");
        }catch (Exception e){
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }

    @RequestMapping("edit")
    @RequiresPermissions("customer:edit")
    @PermissionName("客户编辑")
    @ResponseBody
    public JsonResult edit(Customer customer) {
        JsonResult result = new JsonResult();
<<<<<<< HEAD
        try {
            customerService.updateByPrimaryKey(customer);
            result.setMessage("客户编辑成功");
        } catch (Exception e) {
=======
        try{
            customerService.updateByPrimaryKey(customer);
            result.setMessage("客户编辑成功");
        }catch (Exception e){
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
            e.printStackTrace();
            result.mark("编辑失败");
        }

        return result;
    }

    @RequestMapping("lost")
    @RequiresPermissions("customer:lost")
    @PermissionName("流失客户")
    @ResponseBody
    public JsonResult lost(Long id) {
        JsonResult result = new JsonResult();
<<<<<<< HEAD
        try {
            customerService.lostCustomerById(id);
            result.setMessage("已将客户流失");
        } catch (Exception e) {
=======
        try{
            customerService.lostCustomerById(id);
            result.setMessage("已将客户流失");
        }catch (Exception e){
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }

    @RequestMapping("share")
    @RequiresPermissions("customer:share")
    @PermissionName("移入资源池")
    @ResponseBody
    public JsonResult share(Long id) {
        JsonResult result = new JsonResult();
<<<<<<< HEAD
        try {
            customerService.shareCustomerById(id);
            result.setMessage("已将客户移至资源池");
        } catch (Exception e) {
=======
        try{
            customerService.shareCustomerById(id);
            result.setMessage("已将客户移至资源池");
        }catch (Exception e){
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }

    @RequestMapping("victory")
    @RequiresPermissions("customer:victory")
    @PermissionName("升为正式客户")
    @ResponseBody
    public JsonResult victory(Long id) {
        JsonResult result = new JsonResult();
<<<<<<< HEAD
        try {
            customerService.formalCustomerById(id);
            result.setMessage("恭喜成功升为正式客户！");
        } catch (Exception e) {
=======
        try{
            customerService.formalCustomerById(id);
            result.setMessage("恭喜成功升为正式客户！");
        }catch (Exception e){
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }

    @RequestMapping("absorb")
    @RequiresPermissions("customer:absorb")
    @PermissionName("吸纳资源池客户")
    @ResponseBody
    public JsonResult absorb(Long id) {
        JsonResult result = new JsonResult();
<<<<<<< HEAD
        try {
            customerService.absorbCustomerById(id);
            result.setMessage("已将客户吸纳");
        } catch (Exception e) {
=======
        try{
            customerService.absorbCustomerById(id);
            result.setMessage("已将客户吸纳");
        }catch (Exception e){
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }
<<<<<<< HEAD
}
=======


}
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
